export const getOrgId = () => {
  return parseInt(localStorage.getItem("ORG_ID") || "0",10 );
};

export const setOrgId = (orgId) => {
  return new Promise((resolve) => {
    localStorage.setItem("ORG_ID", orgId);
    resolve();
  });
};

export const setCurrentTeam = (teamId) => {
  return new Promise((resolve) => {
    localStorage.setItem("AI_USER_TEAM_" + getOrgId(), teamId);
    resolve();
  });
};

export const getCurrentTeam = () => {
  return localStorage.getItem("AI_USER_TEAM_" + getOrgId());
};

export const clearCurrentTeam = () => {
  return localStorage.removeItem("AI_USER_TEAM_" + getOrgId());
};

export const setToken = (token) => {
  return new Promise((resolve) => {
    localStorage.setItem("AI_USER_TOKEN_" + getOrgId(), token);
    resolve();
  });
};

export const getToken = () => {
  return localStorage.getItem("AI_USER_TOKEN_" + getOrgId());
};

export const clearToken = () => {
  return localStorage.removeItem("AI_USER_TOKEN_" + getOrgId());
};

export const getUserInfo = () => {
  let userInfo = localStorage.getItem("AI_USER_INFO_" + getOrgId());
  return userInfo ? JSON.parse(userInfo) : null;
};

export const setUserInfo = (userInfo) => {
  return new Promise((resolve) => {
    localStorage.setItem(
      "AI_USER_INFO_" + getOrgId(),
      JSON.stringify(userInfo)
    );
    resolve();
  });
};

export const clearUserInfo = () => {
  return localStorage.removeItem("AI_USER_INFO_" + getOrgId());
};

export const getVillageInfo = () => {
  let villageInfo = localStorage.getItem("AI_VILLAGE_INFO_" + getOrgId());
  return villageInfo ? JSON.parse(villageInfo) : null;
};

export const setVillageInfo = (villageInfo) => {
  return new Promise((resolve) => {
    localStorage.setItem(
      "AI_VILLAGE_INFO_" + getOrgId(),
      JSON.stringify(villageInfo)
    );
    resolve();
  });
};

export const clearVillageInfo = () => {
  return localStorage.removeItem("AI_VILLAGE_INFO_" + getOrgId());
};

export const setUserAccessSource = (source) => {
  return new Promise((resolve) => {
    localStorage.setItem("USER_ACCESS_SOURCE", source);
    resolve();
  });
};

export const setUserIdentity = (user_identity) => {
  return localStorage.setItem("AI_USER_IDENTITY_" + getOrgId(), user_identity);
 
};

export const getUserIdentity = () => {
  return localStorage.getItem("AI_USER_IDENTITY_"+ getOrgId())
};
export const setMenuPermission = (menu_permission) => {
  return localStorage.setItem("MENU_PERMISSION_" + getOrgId(), menu_permission);
};
export const getMenuPermission = () => {
  return localStorage.getItem("MENU_PERMISSION_" + getOrgId());
};
